<?php
class LZ_Search
{
	var $id = 'id';
	var $type = 'type';
	var $type_id = 'type_id';
	var $content = 'content';
	
	var $table = 'search';
	
	function LZ_Search()
	{
		$this->table = LZ_MYSQL_PREFIX.$this->table;
	}

	function add( $type,$type_id,$content)
	{
		global $db;
		$this->delete($type,$type_id);
		$content = string2words($content);
		$sql = make_insert_sql($this->table,array('type'=>$type,'type_id'=>$type_id,'content'=>$content));
		return ($db->query($sql));
	}

	function delete($type,$type_id)
	{
		global $db;
		$sql = "DELETE FROM $this->table WHERE `type` = '$type' && `type_id` = '$type_id';";
		return $db->query($sql);
	}
	
	function search( $type,$keyword ,$limit=500,$record=true,$blur = false)
	{
		global $db;
		$_keyword = $keyword;
		if (!$blur)
		{
			$keywords = string2words($keyword,0);
			$_re = array();
			$t = 0;
			foreach($keywords as $keyword)
			{
				$t++;
				$match = "MATCH(content) AGAINST( '$keyword' )";
				$sql = "SELECT type_id,$match AS `score` FROM $this->table WHERE $match AND `type` = '$type' ORDER BY `score` DESC LIMIT $limit";
				$arr = $db->get_all($sql);
				foreach($arr as $arr2)
				{
					$_re[$arr2['type_id']]++;
				}
			}
			$re = array();
			foreach($_re as $id=>$count)
			{
				if ($count == $t) $re[] = $id;
			}
		}
		else
		{
			$keywords = string2words($keyword);
			$re = array();
			$match = "MATCH(content) AGAINST( '$keywords' )";
			$sql = "SELECT type_id,$match AS `score` FROM $this->table WHERE $match AND `type` = '$type' ORDER BY `score` DESC LIMIT $limit";
			$arr = $db->get_all($sql);
			foreach($arr as $arr2)
			{
				$re[] = $arr2['type_id'];
			}
		}
		
		if (count($re) > 0 && $record)
		{
			include_once(LZ_BASEPATH.'model/hot.php');
			$hot = new LZ_Hot;
			$arr = string2words($_keyword,0,0,1);
			foreach($arr as $k) $hot->add($k);
		}
		return $re;
	}
}
?>